package com.controller;

import com.api.CommonResult;
import com.entity.table.HomePageTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.HomePage;
import com.service.HomePageService;

import java.io.Serializable;
import java.util.List;

/**
 * 首页 控制层。
 *
 * @author admin
 * @since 2024-06-17
 */
@Controller
@RequestMapping("/admin/homePage")
public class HomePageController {

    @Autowired
    private HomePageService homePageService;

    /**
     * 添加首页。
     *
     * @param homePage 首页
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
   // @PostMapping("save")
   // public boolean save(@RequestBody HomePage homePage) {
//        return homePageService.save(homePage);
//    }

    /**
     * 根据主键删除首页。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return homePageService.removeById(id);
    }

    /**
     * 根据主键更新首页。
     *
     * @param homePage 首页
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody HomePage homePage) {
//        return homePageService.updateById(homePage);
//    }

    /**
     * 查询所有首页。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<HomePage> list() {
        return homePageService.list();
    }

    /**
     * 根据首页主键获取详细信息。
     *
     * @param id 首页主键
     * @return 首页详情
     */
    @GetMapping("getInfo/{id}")
    public HomePage getInfo(@PathVariable Serializable id) {
        return homePageService.getById(id);
    }

    /**
     * 分页查询首页。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<HomePage> page(Page<HomePage> page) {
        return homePageService.page(page);
    }

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(HomePage homePage) {
        try{
            boolean r = homePageService.save(homePage);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = homePage.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }

    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(HomePage homePage) {
        try{
            boolean r = homePageService.updateById(homePage);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = homePage.getTitle()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping("List")
    public String typeList(ModelMap model, @RequestParam(value = "homePageTitle",defaultValue = "") String homePageTitle,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(HomePageTableDef.HOME_PAGE.TITLE.like(homePageTitle));
        Page<HomePage> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<HomePage> pageInfo = homePageService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "/back/homePageTitle-list";
    }
    @GetMapping(value = "/addView")
    public  String addUserView(){ return "back/homePageTitle-add";}

    @GetMapping(value = "updateView")
    public String updateView(int id,ModelMap model){
        HomePage homePage = homePageService.getById(id);
        model.addAttribute("homePage",homePage);
        return "/back/homePageTitle-edit";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult update(Integer id) {
        try {
            boolean r = homePageService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
